package com.fjut.kms.service;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by Empathy on 2017/7/15.
 */
public class XMLService {

    public static String generateXMLContents(List<Object> list,String javaClassName){
        String strXML=null;
        try{
            //创建新文档
            if (list==null) return "空记录";
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc =builder.newDocument();
            doc.setXmlStandalone(true);
            Element root = doc.createElement("params");
            doc.appendChild(root);
            Element e=doc.createElement("CheckStatus");
            e.setTextContent("1");
            root.appendChild(e);
            Element license=doc.createElement(javaClassName+"List");
            root.appendChild(license);
            //反射机制获得所有属性
            Class c = Class.forName(javaClassName);
            Method[] methods = c.getMethods();
            Field[] fields = c.getDeclaredFields();
            //遍历集合，一个对象一行
            for (Object obj: list) {
                //节点名称为JAVA文件类名
                Element element = doc.createElement(javaClassName);
                for(Field field:fields){
                    String fieldName = field.getName();
                    //根据属性名合成方法名
                    String methodName ="get"+ fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
                    //动态获取该方法
                    Method method = c.getMethod(methodName, null);
                    //动态调用该方法
                    Object invoke = method.invoke(obj, null);
                    if(invoke==null){
                        invoke ="";
                    }
                    //添加属性
                    Element elementChild=doc.createElement(fieldName);
                    elementChild.setTextContent(invoke.toString());
                    element.appendChild(elementChild);
                }
                license.appendChild(element);
            }
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transFormer = transFactory.newTransformer();
            transFormer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            DOMSource domSource = new DOMSource(doc);

            StringWriter sw = new StringWriter();
            StreamResult xmlResult = new StreamResult(sw);

            transFormer.transform(domSource, xmlResult);
            strXML=sw.toString();
            }catch (Exception e){
            e.printStackTrace();
        }
        return strXML;
    }

    public static String generateXMLerror(String message){
        String strXML=null;
        try{
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc =builder.newDocument();
            doc.setXmlStandalone(true);
            Element root = doc.createElement("error");
            root.setTextContent(message);
            doc.appendChild(root);
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transFormer = transFactory.newTransformer();
            transFormer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            DOMSource domSource = new DOMSource(doc);

            StringWriter sw = new StringWriter();
            StreamResult xmlResult = new StreamResult(sw);

            transFormer.transform(domSource, xmlResult);
            strXML=sw.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return  strXML;
    }


}
